/*
 * Assign.cpp
 *
 *  Created on: 17.04.2012
 *      Author: Eugene Kornykhin
 */

#include "Assign.h"
#include "Number.h"
#include "VarReference.h"
#include <stdexcept>
#include <typeinfo>

Assign::Assign() {

}

Assign::~Assign() {

}

void Assign::execute(Interpreter & interpreter)
{
	try {
		const Number &value = dynamic_cast<const Number &>(interpreter.popPolizElement());
		const VarReference &var = dynamic_cast<const VarReference &>(interpreter.popPolizElement());
		interpreter.setVarValue(var.getVar(), value.getValue());
	} catch(std::bad_cast&) {
		throw std::logic_error("Assign expects a variable reference and a number");
	}
}



PolizPosition Assign::next_position_after(PolizPosition p) const
{
	return p.next();
}



bool Assign::isHalt() const
{
	return false;
}



Assign *Assign::clone(const Poliz &) const
{
	return new Assign();
}



string Assign::toString() const {
	return "[:=]";
}
